package org.wheel.plugins.toolkit.sql.page.impl;

import cn.hutool.core.util.StrUtil;
import org.wheel.plugins.toolkit.sql.page.PaginationStrategy;

import java.util.List;

/**
 * Seek 分页策略 – (create_time, id) > (?, ?)
 */
public class SeekPaginationStrategy implements PaginationStrategy {

    private final List<String> seekColumns; // ["create_time", "id"]

    public SeekPaginationStrategy(List<String> seekColumns) {
        this.seekColumns = seekColumns;
    }

    @Override
    public String generatePaginationQuery(String originalSql, long pageNo, long pageSize) {
        // 直接返回：原 SQL + WHERE (create_time, id) > (?, ?) ORDER BY create_time, id LIMIT ?
        String seek = StrUtil.join(", ", seekColumns);
        return StrUtil.format("{} WHERE ({}) > ({}) ORDER BY {} LIMIT {}", originalSql, seek, seek, seek, pageSize);
    }

//    public void apply(PlainSelect select, List<Object> seekValues, RuleContext ruleContext) {
//        Expression seekExpr = null;
//        for (int i = 0; i < seekColumns.size(); i++) {
//            GreaterThan gt = new GreaterThan();
//            gt.setLeftExpression(new Column(seekColumns.get(i)));
//            gt.setRightExpression(new JdbcNamedParameter(seekColumns.get(i)));
//            seekExpr = (seekExpr == null) ? gt : new AndExpression(seekExpr, gt);
//        }
//
//        Expression where = select.getWhere();
//        select.setWhere(where == null ? seekExpr : new AndExpression(where, seekExpr));
//
//        for (int i = 0; i < seekColumns.size(); i++) {
//            ruleContext.addParam(seekColumns.get(i), seekValues.get(i));
//        }
//    }
}